﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework4
{
    class Program
    {
        static void Main(string[] args)
        {
            GenericList<int> myList = new GenericList<int>();
            for(int i = 0; i < 10; i++)
            {
                myList.Add(i);
            }
            myList.ForEach(w => Console.WriteLine(w));
            int max = 0, min = 0, sum = 0 ;
            myList.ForEach(w => max = w>max? w:max);
            Console.WriteLine("最大值为" + max);
            myList.ForEach(w => min = w < min ? w : min);
            Console.WriteLine("最小值为" + min);
            myList.ForEach(w => sum += w);
            Console.WriteLine("和为" + sum);
        }
    }

    public class Node<T>
    {
        public Node<T> Next { get; set; }
        public T Data { get; set; }

        public Node(T t)
        {
            Next = null;
            Data = t;
        }
}

    public class GenericList<T>
    {
        private Node<T> head;
        private Node<T> tail;

        public GenericList()
        {
            tail = head = null;
        }

        public Node<T> Head
        {
            get => head;
        }
        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                head = tail = n;
            }
            else
            {
                tail.Next = n;
                tail = n;
            }
        }

        public void ForEach(Action<T> action) {
            for (Node<T> node = this.head; node != null; node = node.Next)
            {
                action(node.Data);
            }
        }
    }
}
